import java.awt.*;
import javax.swing.*;
import javax.swing.border.SoftBevelBorder;
import java.awt.event.*;
public class CalendarFrame  extends JFrame implements ActionListener{

    private static final long serialVersionUID = 2089199263529068077L;
    JLabel labelday[] =new JLabel[42];
    JButton titleName[] =new JButton[7];
    String name[] = {"日","一","二","三","四","五","六"};
    JButton nextMonth,prevMonth;
    CalendarBean calendarBean;
    JLabel showMessage = new JLabel("",JLabel.CENTER);
    int year=2011,month=2;
    public CalendarFrame() {
        JPanel pCenter =new JPanel();
        pCenter.setLayout(new GridLayout(7,7));
        for (int i = 0; i < 7; i++) {
            titleName[i]=new JButton(name[i]);
            titleName[i].setBorder(new SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
            pCenter.add(titleName[i]);

        }
        for (int i = 0; i <42; i++) {
            labelday[i]=new JLabel("",JLabel.CENTER);
            labelday[i].setBorder(new SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
            pCenter.add(labelday[i]);

        }
        calendarBean = new CalendarBean();
        nextMonth = new JButton("下月");
        prevMonth = new JButton("上月");
        nextMonth.addActionListener(this);
        prevMonth.addActionListener(this);
        JPanel pNorth = new JPanel(),pSouth = new JPanel();
        pNorth.add(prevMonth);
        pNorth.add(nextMonth);
        pSouth.add(showMessage);
        add(pCenter,BorderLayout.CENTER);
        add(pNorth,BorderLayout.NORTH);
        add(pSouth,BorderLayout.SOUTH);
        setYearAndMonth(year,month);
        validate();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    public void setYearAndMonth(int y,int m) {
        calendarBean.setYear(y);
        calendarBean.setMonth(m);
        String day[] = calendarBean.getCalendar();
        for (int i = 0; i < 42; i++)
            labelday[i].setText(day[i]);
        showMessage.setText("日历："+calendarBean.getYear()+"年"+calendarBean.getMonth()+"月");


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==nextMonth) {
            month=calendarBean.getMonth()+1;
            if(month>12)
                month=1;
            calendarBean.setMonth(month);
            String day[] =calendarBean.getCalendar();
            for (int i = 0; i <42; i++) {
                labelday[i].setText(day[i]);

            }
        }
        else if(e.getSource()==prevMonth) {
            month=calendarBean.getMonth()-1;
            if(month<1)
                month=12;
            calendarBean.setMonth(month);
            String day[] =calendarBean.getCalendar();
            for (int i = 0; i <42; i++) {
                labelday[i].setText(day[i]);
            }
        }
        showMessage.setText("日历："+calendarBean.getYear()+"年"+calendarBean.getMonth()+"月");


    }

}

